import router from './router'
import Cookies from 'js-cookie'
import store from './store'
router.beforeEach(async(to, from, next) => {
    console.log(to.query.id)
        // debugger
    if (Cookies.get('token') && to.query.id) {
        const reslut = await store.dispatch('templateDate/getTemplateData', { id: to.query.id });
        //console.log(reslut)
        const { data } = reslut;
        //data.temp_style = 5;
        if (data.temp_style == 1) {
            if (['/blue', '/yellow', '/green', '/black'].includes(to.path)) {
                next({ path: '/', query: { id: to.query.id } })
            } else {
                next();
            }
        } else if (data.temp_style == 2) {
            if (['/', '/yellow', '/green', '/black'].includes(to.path)) {
                next({ path: '/blue', query: { id: to.query.id } })
            } else {
                next();
            }
        } else if (data.temp_style == 3) {
            if (['/', '/blue', '/green', '/black'].includes(to.path)) {
                next({ path: '/yellow', query: { id: to.query.id } })
            } else {
                next();
            }
        } else if (data.temp_style == 4) {
            if (['/', '/blue', '/yellow', '/black'].includes(to.path)) {
                next({ path: '/green', query: { id: to.query.id } })
            } else {
                next();
            }
        } else if (data.temp_style == 5) {
            if (['/', '/blue', '/yellow', '/green'].includes(to.path)) {
                next({ path: '/black', query: { id: to.query.id } })
            } else {
                next();
            }
        }

    } else {
        if (to.query.st && to.query.id) {
            Cookies.set('token', to.query.st)
            const reslut = await store.dispatch('templateDate/getTemplateData', { id: to.query.id });
            console.log(reslut)
            const { data } = reslut;
            if (data.temp_style == 1) {
                next({ path: '/', query: { id: to.query.id } })
            } else if (data.temp_style == 2) {
                next({ path: '/blue', query: { id: to.query.id } })
            }
        } else {
            Cookies.remove('token')
            if (to.query.id) {
                window.location.href = `https://olpassport.5211game.com/Login?siteid=50008&returnUrl=${escape(window.location.href)}&srvid=7`
            } else {
                window.location = "https://www.5211game.com";
            }

        }
    }

})